
const app = getApp(); 
Page({
  data: {
    isSearch: false,
    result: [],
    value: '',
    hotCity:  ['北京市', '深圳', '广州', '哈尔滨','厦门','上海'],
    history: []
  },
  inputHandle(e) {
    const { value } = e.detail
    if (value) {
      var that =this
      wx.request({
        url: 'https://geoapi.qweather.com/v2/poi/lookup',
        data:{
          key:'7364f1d6588041229b5a69633d058c95',
          location:value,
          type:"scenic"
        },
        success:function (res) {
          
          if(res.data.code==200){
            var lat = res.data.poi[0].lat
            var lon = res.data.poi[0].lon
            wx.reLaunch({
            url: '/pages/index/index?lat='+lat+'&lon='+lon,
            })
          }else if(res.data.code==404){
            wx.showModal({
              title: '提示',
              content: '内容不需要加市例如“厦门”或者输入城市不存在，*部分城市无法获取天气',
              success: function (res) {
                if(res.confirm){
                  that.clearHandle();
                }
              }
            })
          }
        },
        fail:function(res){
          
        },
      })
      // wx.navigateTo({
      //   url: '/page/index/index?vule',
      // })
    }
  },
  onLoad() {
    this.setData({
      history: this.getLocal()
    })
    // console.log("--------",app.globalData.hotCity);
  },
  cancelHandle() {
    wx.navigateBack()
  },
  selectItem(e) {
    const { lat, lng } = e.currentTarget.dataset
    wx.navigateTo({
      url: `pages/index/index`
    })
  },
  clearHandle() {
    this.setData({
      value: ''
    })
  },
  // clearHistory() {
  //   wx.removeStorage({ key: 'history'})
  //   this.setData({
  //     history: []
  //   })
  // },
  dwHandle() {
    wx.reLaunch({
      url: `/pages/index/index`
    })
  },
  hotHandle(e) {
    const { value } = e.currentTarget.dataset
    this.setLocal(value)
    wx.request({
      url: 'https://geoapi.qweather.com/v2/poi/lookup',
      data:{
        key:'7364f1d6588041229b5a69633d058c95',
        location:value,
        type:"scenic"
      },
      success:function (res) {
        console.log('------------------------------------',res)
        var lat = res.data.poi[0].lat
        var lon = res.data.poi[0].lon
        wx.reLaunch({
        url: '/pages/index/index?lat='+lat+'&lon='+lon,
        })
      }
    })
  },
  // 设置历史记录
  setLocal(val) {
    const arr = this.getLocal()
    if (!arr.includes(val)) {
      console.log(arr)
      arr.push(val)
      wx.setStorageSync('history', arr)
    }
  },
  getLocal() {
    return wx.getStorageSync('history') ? wx.getStorageSync('history') : []
  },
  delHandle(e) {
    const { value } = e.currentTarget.dataset
    const arr = this.getLocal()
    if (arr.includes(value)) {
      const index = arr.indexOf(value)
      arr.splice(index, 1)
      wx.setStorageSync('history', arr)
      this.setData({
        history: arr
      })
      
    }
  }
})